package com.ruoyi.youtian.controller;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.youtian.domain.vo.TEarlyWarningVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.youtian.domain.TEarlyWarning;
import com.ruoyi.youtian.service.ITEarlyWarningService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警设置Controller
 * 
 * @author ruoyi
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/earlyWarning")
public class TEarlyWarningController extends BaseController
{
    private String prefix = "earlyWarning/earlyWarning";

    @Autowired
    private ITEarlyWarningService tEarlyWarningService;

    @RequiresPermissions("earlyWarning:earlyWarning:view")
    @GetMapping()
    public String earlyWarning()
    {
        return prefix + "/earlyWarning";
    }

    /**
     * 查询预警设置列表
     */
    @RequiresPermissions("earlyWarning:earlyWarning:list")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list2(TEarlyWarningVo tEarlyWarningVo)
    {
        startPage();
        List<TEarlyWarningVo> list = tEarlyWarningService.selectEarlyWarningVoList(tEarlyWarningVo);
        list.forEach(t->{
            if (t.getDays() == null) {
                t.setDays(0);
            }
        });
        return getDataTable(list);
    }


    /**
     * 新增预警设置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

//    /**
//     * 新增保存预警设置
//     */
//    @RequiresPermissions("earlyWarning:earlyWarning:add")
//    @Log(title = "预警设置", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(TEarlyWarning tEarlyWarning)
//    {
//        tEarlyWarning.setCreateBy(getLoginName());
//        return toAjax(tEarlyWarningService.insertTEarlyWarning(tEarlyWarning));
//    }


    /**
     * 新增保存预警设置(功能修改)
     */
    @RequiresPermissions("earlyWarning:earlyWarning:addupda")
    @Log(title = "预警设置（功能修改）", businessType = BusinessType.INSERT)
    @PostMapping("/addupda")
    @ResponseBody
    public AjaxResult addSaves(@RequestBody TEarlyWarning tEarlyWarning)
    {
        if (tEarlyWarning.getDay() > 365){
            return AjaxResult.error("天数不能超过365天");
        }
        tEarlyWarning.setCreateBy(getLoginName());
        return toAjax(tEarlyWarningService.reinsertTEarlyWarning(tEarlyWarning));
    }

    /**
     * 修改预警设置
     */
    @RequiresPermissions("earlyWarning:earlyWarning:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TEarlyWarning tEarlyWarning = tEarlyWarningService.selectTEarlyWarningById(id);
        mmap.put("tEarlyWarning", tEarlyWarning);
        return prefix + "/edit";
    }

    /**
     * 修改保存预警设置
     */
    @RequiresPermissions("earlyWarning:earlyWarning:edit")
    @Log(title = "预警设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody TEarlyWarning tEarlyWarning)
    {
        return toAjax(tEarlyWarningService.updateTEarlyWarning(tEarlyWarning));
    }

    /**
     * 删除预警设置
     */
    @RequiresPermissions("earlyWarning:earlyWarning:remove")
    @Log(title = "预警设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tEarlyWarningService.deleteTEarlyWarningByIds(ids));
    }


    /**
     * 预警设置启用状态修改
     */
    @RequiresPermissions("earlyWarning:earlyWarning:remove")
    @Log(title = "预警设置", businessType = BusinessType.DELETE)
    @PostMapping( "/changeEarlyWaringStatus")
    @ResponseBody
    public AjaxResult changeEarlyWaringStatus(String ids, String status)
    {
        int res = tEarlyWarningService.changeEarlyWaringStatus(ids, status);
        if(res > 0 ){
           return AjaxResult.success(res);
        }else{
            return  AjaxResult.error();
        }
    }
}
